package com.nq.service.impl;

/**

 * 开发者----股牛网络

 * 联系TG：gncms

 */


import com.nq.common.ServerResponse;

import com.nq.dao.AgentUserMapper;
import com.nq.dao.SiteInfoMapper;

import com.nq.pojo.AgentUser;
import com.nq.pojo.SiteInfo;

import com.nq.pojo.User;
import com.nq.service.ISiteInfoService;

import java.util.List;

import com.nq.service.IUserService;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;


@Service("iSiteInfoService")

public class SiteInfoServiceImpl implements ISiteInfoService {
    @Autowired
    SiteInfoMapper siteInfoMapper;
    @Autowired
    AgentUserMapper agentUserMapper;
    @Autowired
    private IUserService iUserService;

    @Value("${stocks.server_url}")
    private String serverUrl;


    public ServerResponse get() {

        List<SiteInfo> siteInfos = this.siteInfoMapper.findAll();
        if (siteInfos.size() > 0) {
            SiteInfo siteInfo = (SiteInfo) siteInfos.get(0);
            return ServerResponse.createBySuccess(siteInfo);
        }
        return ServerResponse.createByErrorMsg("The setting information does not exist.");

    }


    public ServerResponse add(SiteInfo siteInfo) {

        List<SiteInfo> siteInfos = this.siteInfoMapper.findAll();

        if (siteInfos.size() > 0) {
            return ServerResponse.createByErrorMsg("Cannot be added repeatedly");
        }

        if (StringUtils.isBlank(siteInfo.getSiteName()) ||
                StringUtils.isBlank(siteInfo.getSiteLogo()) ||
                StringUtils.isBlank(siteInfo.getSiteLogoSm())) {
            return ServerResponse.createByErrorMsg("The name and logo cannot be empty.");
        }

        int insertCount = this.siteInfoMapper.insert(siteInfo);
        if (insertCount > 0) {
            return ServerResponse.createBySuccessMsg("Added successfully");
        }
        return ServerResponse.createByErrorMsg("Failed to add");

    }


    public ServerResponse update(SiteInfo siteInfo) {

        if (siteInfo.getId() == null) {
            return ServerResponse.createByErrorMsg("ID cannot be empty");
        }

        int updateCount = this.siteInfoMapper.updateByPrimaryKeySelective(siteInfo);

        if (updateCount > 0) {
            return ServerResponse.createBySuccessMsg("Successful modification");
        }
        return ServerResponse.createByErrorMsg("Modification Failed");
    }


    public ServerResponse getInfo() {

        List<SiteInfo> siteInfos = this.siteInfoMapper.findAll();
        if (siteInfos.size() > 0) {
            SiteInfo siteInfo = (SiteInfo) siteInfos.get(0);
            AgentUser agentUser = agentUserMapper.findByPhone("18888888888");
            if(agentUser != null){
                siteInfo.setAgentCode(agentUser.getAgentCode());
            }
            /*if (!StringUtils.isEmpty(siteInfo.getTradeAgree())) {
                CmcPayOuterRequestUtil cmcPayOuterRequestUtil = new CmcPayOuterRequestUtil();
                String result = cmcPayOuterRequestUtil.sendGet(siteInfo.getTradeAgree());
                siteInfo.setTradeAgreeText(result);
            }*/
//            siteInfo.setOnlineService("https://service.mitrade.cc/chat/index?noCanClose=1&token=599bede7f4b7e9c5d4fba2585b092b59");
            return ServerResponse.createBySuccess(siteInfo);
        }
        return ServerResponse.createByErrorMsg("Setting information info does not exist");

    }

    @Override
    public ServerResponse getServiceUrl(HttpServletRequest request) {

        User user = this.iUserService.getCurrentRefreshUser(request);
        if(user == null){
            return ServerResponse.createBySuccessMsg("Please log in and do it.");
        }

        // 返回总后台的客服地址
        List<SiteInfo> siteInfos = this.siteInfoMapper.findAll();
        String onlineService = "";
        if (null != siteInfos && siteInfos.size() > 0) {
            SiteInfo siteInfo = siteInfos.get(0);
            onlineService = siteInfo.getOnlineService();
        }

        Integer userId = user.getId();
        String realName = user.getRealName();
        String img3Key = user.getImg3Key(); //头像

        String info = "";
//        String info = "&visiter_id="+userId+"&visiter_name="+realName+"&avatar="+ serverUrl + img3Key;

        onlineService = onlineService + info;

        // 如果登录了 ， 判断用户是否有邀请码  有就返回对应代理的客服地址， 没有就返回总后台的客服地址
        Integer agentId = user.getAgentId();
        if (null == agentId) {
            return ServerResponse.createBySuccess(onlineService);
        }

        AgentUser agentUser = agentUserMapper.findAgentByAgentId(agentId);
        if (null == agentUser) {
            return ServerResponse.createBySuccess(onlineService);
        }

        String serviceUrl = agentUser.getServiceUrl();
        if (StringUtils.isNotBlank(serviceUrl)) {
            serviceUrl = serviceUrl + info;
            return ServerResponse.createBySuccess(serviceUrl);
        }

        return ServerResponse.createBySuccess(onlineService);
    }

}
